<script setup>
import { ref } from 'vue'
import useTable from "@/utils/useTable";
import { http } from "@/api";
import {useRouter} from "vue-router";
import {ElMessage} from "element-plus";
import {Plus} from "@element-plus/icons-vue";
const router = useRouter()
/**
 * 搜索
 */

const dateRange = ref([])
const changeDate = (val) => {
  if (!val) {
    searchParams.value.startTime = ''
    searchParams.value.endTime = ''
    return
  }
  searchParams.value.startTime = val[0] || ''
  searchParams.value.endTime = val[1] || ''
}
/**
 * 表格
 * @type {Ref<UnwrapRef<*[]>>}
 */
const {
  searchParams,
  pageNo,
  pageSize,
  total,
  changePage,
  changeSize,
  dataSource,
  search,
  reset,
  openAdd,
  openView,
  openEdit,
  openDelete
} = useTable({
  requestFn: http.UserDynamic,
  routerPre: 'ActivityPublish',
  params: {
    "classify": 4,
    "endTime": "",
    "startTime": "",
    "userName": ""
  },
})
const updateTableRow = (row) => {
  http.UserDynamic.updateIndex({
    id: row.id,
    sort: row.sort
  }).then(res => {
    ElMessage.success('排序成功')
  })
}
const changeStatus = (row) => {
  let status = ''
  if (row.status === 1) {
    status = 0
  } else {
    status = 1
  }
  http.UserDynamic.updateStatus({
    id: row.id,
    status
  }).then(res => {
    ElMessage.success('操作成功')
    row.status = status

  })
}


</script>


<template>
  <PageMain title="活动发布">
    <el-form label-suffix="：" inline>
      <el-form-item label="用户昵称">
        <el-input clearable v-model="searchParams.userName"></el-input>
      </el-form-item>

      <el-form-item label="创建时间">
        <el-date-picker clearable
            @change="changeDate"
            v-model="dateRange"
            type="daterange"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="add-box">
      <el-button type="primary" :icon="Plus" @click="openAdd">添加</el-button>
    </div>
    <div class="table-box">
      <el-table :data="dataSource">
        <el-table-column label="用户昵称" property="nick"></el-table-column>
        <el-table-column label="头像">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <ViewImage type="avatar" :image-url="scope.row.imageUrl"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="标题" property="title"/>
        <el-table-column label="发布时间" width="180px" prop="releaseTime"/>
        <el-table-column label="点赞数" width="100px" prop="like"/>
        <el-table-column label="收藏数" prop="collect"/>
        <el-table-column label="排序" width="80px">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <el-input placeholder="请输入排序" v-model="scope.row.sort" type="number" @blur="updateTableRow(scope.row)"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" width="180px" prop="createTime"></el-table-column>
        <el-table-column label="操作" fixed="right" width="200px">
          <template #default="scope">
            <el-button type="text" @click="changeStatus(scope.row)">{{ scope.row.status === 1? '撤销违规' : '违规'}}</el-button>
            <el-button type="text" @click="openView(scope.row)">查看</el-button>
            <el-button type="text" @click="openDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </PageMain>
</template>

<style scoped lang="scss">

</style>
